﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Ray_Tracer
{
    public partial class Form1 : Form
    {
        List<ColoredDisk> disks;
        Bitmap img;

        public Form1()
        {
            InitializeComponent();
            disks = new List<ColoredDisk>();
            textBox1.Text += "x";
            textBox2.Text += "y";
            textBox3.Text += "radius";

            InitializeImg();
        }

        private void AddDisk_Click(object sender, EventArgs e)
        {
            try
            {
                CreateDisk();
                DrawDisk(disks[disks.Count - 1]);
                SetCuteBox();
            }
            catch 
            {
                Error("Erreur, vous devez rentrer des entiers.");
            }
        }

        private void Error(string str)
        {
            textBox1.Clear();
            textBox1.Text += "x";
            textBox2.Clear();
            textBox2.Text += "y";
            textBox3.Clear();
            textBox3.Text += "radius";

            messageBox.Clear();
            messageBox.Text += str;
        }

        public void CreateDisk()
        {
            int x, y, r;
            x = Convert.ToInt16(textBox1.Text);
            y = Convert.ToInt16(textBox2.Text);
            r = Convert.ToInt16(textBox3.Text);
            ColoredDisk disk = new ColoredDisk(x, y, r);

            disks.Add(disk);

            messageBox.Clear();
            messageBox.Text += "Le rayon du disque d se trouvant en (" + x + ", " + y + ") est " + r + ".";
        }

        private void DrawDisk(ColoredDisk disk)
        {
            for (int i = 0; i < img.Width; i++)
            {
                for (int j = 0; j < img.Height; j++)
                {
                    if (disk.is_in_bound(i, j))
                        img.SetPixel(i, j, disk.color);
                }
            }
        }

        public void InitializeImg()
        {
            img = new Bitmap(700, 450);

            for (int i = 0; i < img.Width; i++)
            {
                for (int j = 0; j < img.Height; j++)
                {
                    img.SetPixel(i, j, Color.White);
                }
            }

            SetCuteBox();
        }

        public void SetCuteBox()
        {
            cuteBox.Image = img;
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            disks.Clear();

            InitializeImg();

            SetCuteBox();
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            try
            {
                disks.RemoveAt(disks.Count - 1);

                InitializeImg();

                for (int i = 0; i < disks.Count; i++)
                {
                    DrawDisk(disks[i]);
                }

                SetCuteBox();
            }
            catch 
            {
                Error("Il n'y a plus de disque.");
            }
        }
    }
}
